import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:lianwenke20220712/bean/news_list.dart';
import 'package:lianwenke20220712/http/httpUtils.dart';
import 'package:lianwenke20220712/news_info.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:umeng_common_sdk/umeng_common_sdk.dart';

/// FileName 连文科
///
/// @Author/// @Date 2022/7/12 14:55
///
/// @Description 图片页面的新闻列表
class PicNewsWidget extends StatefulWidget {
  const PicNewsWidget({Key key}) : super(key: key);

  @override
  _PicNewsWidgetState createState() => _PicNewsWidgetState();
}

class _PicNewsWidgetState extends State<PicNewsWidget> with AutomaticKeepAliveClientMixin{
  int page =1,size = 5;
  RefreshController _refreshController;
  List<Result> _list = [];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _refreshController = RefreshController();
    NewsBean();
  }
  //避免内存泄漏
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _refreshController.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SmartRefresher(
        controller: _refreshController,
        enablePullUp: true,
        enablePullDown: true,
        onRefresh: (){
          page =1;
          NewsBean();
          _refreshController.refreshCompleted();
        },
        onLoading: (){
          page++;
          NewsBean();
          _refreshController.loadComplete();
        },
        // ignore: missing_return
        child: _list == null ? Center(child: CircularProgressIndicator(),) : ListView.builder(itemBuilder: (BuildContext context, int index){
          //判断是否是广告
          if(_list[index].whetherAdvertising == 2){
            return Container(
              height: 200,
              width: MediaQuery.of(context).size.width,
              margin: EdgeInsets.symmetric(horizontal: 10,vertical: 5),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(15),
                color: Colors.white
              ),
              child: CachedNetworkImage(imageUrl: _list[index].infoAdvertisingVo.adImageUrl,fit: BoxFit.fill,height: MediaQuery.of(context).size.height,),
            );
          }else{
            var thumbnailUrl = _list[index].thumbnailUrl;
            var split = thumbnailUrl.split(';');
            if(split.length > 1){
              return InkWell(
                onTap: (){
                  Navigator.of(context).push(MaterialPageRoute(builder: (context){
                    return NewsInfoWidget(id: _list[index].newsId,);
                  }));
                  //友盟埋点
                  UmengCommonSdk.onEvent('Umeng', {'userId':0});
                },
                child: Container(
                  height: 200,
                  width: MediaQuery.of(context).size.width,
                  margin: EdgeInsets.symmetric(horizontal: 10,vertical: 5),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(15),
                      color: Colors.white
                  ),
                  child: Column(
                    children: [
                      Text(_list[index].title),
                      Row(
                        children: [
                          Expanded(
                            child: Container(
                              height: 100,
                              margin: EdgeInsets.symmetric(horizontal: 5,vertical: 10),
                              child: CachedNetworkImage(imageUrl: split[0],),
                            ),
                          ),
                          Expanded(
                            child: Container(
                              height: 100,
                              margin: EdgeInsets.symmetric(horizontal: 5,vertical: 10),
                              child: CachedNetworkImage(imageUrl: split[1],),
                            ),
                          ),
                          Expanded(
                            child: Container(
                              height: 100,
                              margin: EdgeInsets.symmetric(horizontal: 5,vertical: 10),
                              child: CachedNetworkImage(imageUrl: split[2],),
                            ),
                          ),
                        ],
                      )
                    ],
                  ),
                ),
              );
            }else if(split.length == 1){
              return InkWell(
                onTap: (){
                  Navigator.of(context).push(MaterialPageRoute(builder: (context){
                    return NewsInfoWidget(id: _list[index].newsId,);
                  }));
                  //友盟埋点
                  UmengCommonSdk.onEvent('Umeng', {'userId':0});
                },
                child: Container(
                  height: 200,
                  width: MediaQuery.of(context).size.width,
                  margin: EdgeInsets.symmetric(horizontal: 10,vertical: 5),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(15),
                      color: Colors.white
                  ),
                  child: Column(
                    children: [
                      Text(_list[index].title),
                          Container(
                            height: 100,
                            margin: EdgeInsets.symmetric(horizontal: 5,vertical: 10),
                            child: CachedNetworkImage(imageUrl: split[0],fit: BoxFit.fill,),
                          ),
                    ],
                  ),
                ),
              );
            }
          }
        },itemCount: _list.length,),
      ),
    );
  }

  void NewsBean() {
    HttpUtils().MendGet('http://mobile.bwstudent.com/hotNews/news/v1/findColumnNewsList',isCache: true,map: {'columnId':1001,'page':page,'size':size},onSuccess: (data){
        var result = NewsList.fromJson(data).result;
        if(page == 1){
          _list.clear();
        }
        if(result.length == null || result.length < size){
          _refreshController.loadNoData();
        }else{
          _refreshController.resetNoData();
        }

        setState(() {
          _list.addAll(result);
        });
    });
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
